<?php

/**
 * MyHammer IndexedMemcache
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License Version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.myhammer.de/opensource/license/gpl.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opensource@myhammer.de so we can send you a copy immediately.
 *
 * @category   MyHammer
 * @package    IndexedMemcache
 * @subpackage Memcache
 * @license    http://www.myhammer.de/opensource/license/gpl.txt GNU General Public License Version 3
 * @version    1.0
 * @author     Steven Berg <berg@myhammer.de>
 */

class cMemCacheDataContainer {
    const CLASS_NAME = 'c';
    const GUID = 'g';
    const LINK_ID = 'l';
    const DATA = 'd';

    /**
     * encode MemCacheModel
     *
     * @param cMemCacheModel $oMemCacheModel
     * @return string
     */
    public static function encode(cMemCacheModel $oMemCacheModel) {
        $aJsonData = array();
        $aJsonData[self::CLASS_NAME] = $oMemCacheModel->getClassName();
        $aJsonData[self::GUID] = $oMemCacheModel->getGuId();
        $aJsonData[self::LINK_ID] = $oMemCacheModel->getLinkId();
        $aJsonData[self::DATA] = $oMemCacheModel->getColumns();

        return json_encode($aJsonData);
    }

    /**
     * decode JsonString
     *
     * @param string $sDataContainer
     * @return cMemCacheModel
     */
    public static function decode($sDataContainer) {
        $oDataContainer = json_decode($sDataContainer);

        $sClassName = self::CLASS_NAME;
        $sLinkId = self::LINK_ID;
        $sGuId = self::GUID;
        $aData = self::DATA;

        $oObject = new $oDataContainer->$sClassName;
        $oObject->setGuId($oDataContainer->$sGuId);
        $oObject->setLinkId($oDataContainer->$sLinkId);
        $oObject->setData($oDataContainer->$aData);
        return $oObject;
    }
}